using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public class BackPackSwitchPage : MonoBehaviour
{
	
	public UICenterOnChild centerOnChild;
	public Transform shineTip;
	public Transform blackTips;
	GameObject centerGameObject;
	UIDraggablePanel mDrag;
	UISprite[] blackTipArray ;
	string pagePre = "Page_";
	Transform[] pages = new Transform[6];
	
	// Use this for initialization
	void Start ()
	{
		if (centerOnChild == null)
			return;
		
		if (mDrag == null)
			mDrag = NGUITools.FindInParents<UIDraggablePanel> (centerOnChild.gameObject);
		
		if (mDrag == null) {
			Debug.LogWarning (GetType () + " requires " + typeof(UIDraggablePanel) + " on a parent object in order to work", this);
		} else {
			mDrag.onDragFinished += OnDragFinished;
		}
		
		blackTipArray = blackTips.GetComponentsInChildren<UISprite> ();
		
		for (int i = 0; i < 6; i++) {
			string pageName = pagePre + i;
			pages [i] = centerOnChild.transform.Find (pageName);
		}
		
		OnDragFinished ();
	}
	
	void OnDragFinished ()
	{
		centerGameObject = centerOnChild.centeredObject;
		
		int index = 0;
		if (centerGameObject != null) {
			string centerObjectName = centerGameObject.name;
			index = int.Parse (centerObjectName.Substring (centerObjectName.Length - 1));
		} else {
			index = 0;
		}
		
		shineTip.localPosition = blackTipArray [index].transform.localPosition;
		
		//将本页以外的collider全部禁掉//
		foreach (Transform t in pages) {
			
			BoxCollider[] boxColliders = t.GetComponentsInChildren<BoxCollider> ();
			if (t == centerGameObject.transform) {
				for (int i = 0; i < boxColliders.Length; i++) {
					boxColliders [i].enabled = true;
				}
			} else {
				for (int i = 0; i < boxColliders.Length; i++) {
					boxColliders [i].enabled = false;
				}
			}
		}
	}
	
	// Update is called once per frame
	void Update ()
	{
	
	}
}
